package com.ruoyi.cms.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.cms.domain.Article;
import com.sun.jna.platform.unix.solaris.LibKstat;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class PuUtil {


    public static String dirPath;
    @Value("${cms.file.upload.path}")
    public void setDirPath(String dirPath) {
        PuUtil.dirPath = dirPath;
    }

    public static Article updateArticleToKey(Article article, String key){
        String content = article.getContent();
        content = changeKey(content,"C",key);
        article.setContent(content);
        return article;
    }

    private static JSONObject getChoursMap(){
        File cfgFile = null;
        JSONObject jsonObject = new JSONObject();

        String jsonString = "{\"A\":[\"A\",\"#A/bB\",\"B\",\"C\",\"#C/bD\",\"D\",\"#D/bE\",\"E\",\"#F/bG\",\"#F/bG\",\"G\",\"#G/bA\"],\"B\":[\"B\",\"C\",\"#C/bD\",\"D\",\"#D/bE\",\"E\",\"#F/bG\",\"#F/bG\",\"G\",\"#G/bA\",\"A\",\"#A/bB\"],\"C\":[\"C\",\"#C/bD\",\"D\",\"#D/bE\",\"E\",\"F\",\"#F/bG\",\"G\",\"#G/bA\",\"A\",\"#A/bB\",\"B\"],\"D\":[\"D\",\"#D/bE\",\"E\",\"F\",\"#F/bG\",\"G\",\"#G/bA\",\"A\",\"#A/bB\",\"B\",\"C\",\"#C/bD\"],\"#G/bA\":[\"#G/bA\",\"A\",\"#A/bB\",\"B\",\"C\",\"#C/bD\",\"D\",\"#D/bE\",\"E\",\"#F/bG\",\"#F/bG\",\"G\"],\"L1\":[\"L1\",\"#L1/bL2\",\"L2\",\"#L2/bL3\",\"L3\",\"L4\",\"#L4/bL5\",\"L5\",\"#L5/bL6\",\"L6\",\"#L6/bL7\",\"L7\"],\"E\":[\"E\",\"F\",\"#F/bG\",\"G\",\"#G/bA\",\"A\",\"#A/bB\",\"B\",\"C\",\"#C/bD\",\"D\",\"#D/bE\"],\"F\":[\"F\",\"#F/bG\",\"G\",\"#G/bA\",\"A\",\"#A/bB\",\"B\",\"C\",\"#C/bD\",\"D\",\"#D/bE\",\"E\"],\"G\":[\"G\",\"#G/bA\",\"A\",\"#A/bB\",\"B\",\"C\",\"#C/bD\",\"D\",\"#D/bE\",\"E\",\"#F/bG\",\"#F/bG\"],\"#A/bB\":[\"#A/bB\",\"B\",\"C\",\"#C/bD\",\"D\",\"#D/bE\",\"E\",\"#F/bG\",\"#F/bG\",\"G\",\"#G/bA\",\"A\"],\"#F/bG\":[\"#F/bG\",\"G\",\"#G/bA\",\"A\",\"#A/bB\",\"B\",\"C\",\"#C/bD\",\"D\",\"#D/bE\",\"E\",\"F\"],\"#C/bD\":[\"#C/bD\",\"D\",\"#D/bE\",\"E\",\"F\",\"#F/bG\",\"G\",\"#G/bA\",\"A\",\"#A/bB\",\"B\",\"C\"],\"#D/bE\":[\"#D/bE\",\"E\",\"F\",\"#F/bG\",\"G\",\"#G/bA\",\"A\",\"#A/bB\",\"B\",\"C\",\"#C/bD\",\"D\"]}";
        if (jsonString != null){
            jsonObject = JSONObject.parseObject(jsonString);
            return jsonObject;
        }

        try {
//            cfgFile = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "chours.xlsx");
            InputStream in =  new FileInputStream(dirPath+ "/chours.xlsx");
            Workbook workbook = new XSSFWorkbook(in);
            Sheet sheet = workbook.getSheetAt(0);
            for (int j = 0; j < sheet.getPhysicalNumberOfRows(); j++) {//获取每行
                XSSFRow row= (XSSFRow) sheet.getRow(j);
                JSONArray item = new JSONArray();
                for(int k = 0; k < row.getPhysicalNumberOfCells(); k++){//获取每个单元格

                    System.out.print(row.getCell(k).getStringCellValue()+"\t");
                    item.add(row.getCell(k).getStringCellValue());
                }

                jsonObject.put((String) item.get(0),item);
                System.out.println("");
            }
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            return jsonObject;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }


    private static JSONArray getChours(JSONObject choursMap,String key){


        if (choursMap.containsKey(key)){
            return choursMap.getJSONArray(key);
        }


        for (String mapKey : choursMap.keySet()
                ) {
            if (mapKey.toLowerCase().contains(key.toLowerCase())){
                return choursMap.getJSONArray(mapKey);
            }
        }
        return null;
    }


    public static String changeKey(String content,String originKey,String pickKey){
        JSONObject jsonObject = getChoursMap();

//        String originKey = originKey; //原调
//        String pickKey = "#C"; //选调


        JSONArray originChours =getChours(jsonObject,originKey);//获取原调和弦

        JSONArray pickChours =getChours(jsonObject,pickKey);//获取选调和弦


        JSONArray choursStep =getChours(jsonObject,"L1");//和弦级数

        String originContent = content;
        System.out.println("原调\r\n"+originContent);
        for (int i = 0; i < originChours.size(); i ++) {
            String[] originChourItems = originChours.getString(i).split("/");
            for (String originChour:originChourItems
            ) {
                String step = choursStep.getString(i);

                {
                    String reg = "(\\["+originChour.trim()+")";
                    originContent = originContent.replaceAll(reg,"["+step+"");
                }

                {
                    String reg = "(\\/"+originChour.trim()+")";
                    originContent = originContent.replaceAll(reg,"["+step+"");
                }

                {
                    String reg = "( |&nbsp;|<p>)"+originChour.trim()+"( |&nbsp;|</p>)";

                    originContent = originContent.replaceAll(reg," "+step+" ");
                }
                {
                    String reg = ""+originChour.trim()+"( |&nbsp;|</p>)";
                    originContent = originContent.replaceAll(reg,""+step+" ");

                }
                {
                    String reg = "( |&nbsp;|<p>)"+originChour.trim()+"";
                    originContent = originContent.replaceAll(reg," "+step+"");
                }


            }


        }

        System.out.println("级数\r\n"+originContent);
        for (int i = 0; i < originChours.size(); i ++) {
            String pickChour = pickChours.getString(i);
            pickChour = pickChour.split("/")[0];
            String step = choursStep.getString(i);

            JSONObject switchDict = new JSONObject();
            switchDict.put("bD","#C");
            switchDict.put("bG","#F");
            switchDict.put("bA","#G");
            switchDict.put("#A","bB");
            switchDict.put("#D","bE");
            if (switchDict.containsKey(pickChour)){
                pickChour = switchDict.getString(pickChour);
            }

            originContent = originContent.replaceAll(step,pickChour);
        }

        System.out.println("变调\r\n"+originContent);
        return originContent;
    }

    @Test
    public  void test(){
        String originContent = "[C]哦对司法[Amsus7/G7]路上看到吗设计[Em7]";
        String newContent = changeKey(originContent,"C","F");
        System.out.println(newContent);
    }
}
